package cn.com.bluemoon.daps.sync.core.sync.sql.mysql;

import cn.com.bluemoon.daps.common.toolkit.BmAssetUtils;
import cn.com.bluemoon.daps.model.entity.DapDataModelSubordinateKey;
import cn.com.bluemoon.daps.model.entity.DapDataModelTable;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableField;
import cn.com.bluemoon.daps.model.entity.DapDataModelTablePartition;
import cn.com.bluemoon.daps.sync.base.BmScript;
import cn.com.bluemoon.daps.sync.core.sync.sql.AbstractSql;
import cn.com.bluemoon.daps.sync.core.sync.sql.IBmSql;
import cn.com.bluemoon.daps.sync.core.sync.sql.ptsql.MysqlPtSql;
import com.alibaba.druid.DbType;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * mysql sql 生成器
 *
 * @author Jarod Kong
 * @date 2021-01-15 23:09
 **/
@Slf4j
public class MysqlBmSql extends AbstractSql {
    private final IBmSql ptSql, ddlSql;

    public MysqlBmSql() {
        super(DbType.mysql);
        ptSql = new MysqlPtSql();
        ddlSql = new MysqlDdlSql();
    }

    @Override
    public BmScript dropTable(String oldTableName) {
        BmAssetUtils.notNull(oldTableName);
        return BmScript.builder()
                .sql(ddlSql.dropTable(oldTableName))
                .ptSql(ptSql.dropTable(oldTableName))
                .build();
    }

    @Override
    public BmScript createTable(DapDataModelTable newT,
                                List<DapDataModelTableField> newFs,
                                List<DapDataModelSubordinateKey> newIs,
                                List<DapDataModelTablePartition> newPs) {
        return BmScript.builder()
                .sqls(ddlSql.createTable(newT, newFs, newIs, newPs))
                .ptSqls(ptSql.createTable(newT, newFs, newIs, newPs))
                .build();
    }

    @Override
    public BmScript changeTable(DapDataModelTable oldT, DapDataModelTable newT) {
        return BmScript.builder()
                .sqls(ddlSql.changeTable(oldT, newT))
                .ptSqls(ptSql.changeTable(oldT, newT))
                .build();
    }

    @Override
    public BmScript createField(DapDataModelTable newT, DapDataModelTableField newF, String preFieldName) {
        return BmScript.builder()
                .sql(ddlSql.createField(newT, newF, preFieldName))
                .ptSql(ptSql.createField(newT, newF, preFieldName))
                .build();
    }

    @Override
    public BmScript dropField(DapDataModelTable oldT, DapDataModelTableField oldF) {
        return BmScript.builder()
                .sql(ddlSql.dropField(oldT, oldF))
                .ptSql(ptSql.dropField(oldT, oldF))
                .build();
    }

    @Override
    public BmScript changeField(DapDataModelTable newT, DapDataModelTableField oldF, DapDataModelTableField newF, String preFieldName) {
        return BmScript.builder()
                .sql(ddlSql.changeField(newT, oldF, newF, preFieldName))
                .ptSql(ptSql.changeField(newT, oldF, newF, preFieldName))
                .build();
    }

    @Override
    public BmScript dropIndex(DapDataModelTable oldT, DapDataModelSubordinateKey oldI) {
        return BmScript.builder()
                .sql(ddlSql.dropIndex(oldT, oldI))
                .ptSql(ptSql.dropIndex(oldT, oldI))
                .build();
    }

    @Override
    public BmScript createIndex(DapDataModelTable newT, DapDataModelSubordinateKey newI) {
        return BmScript.builder()
                .sql(ddlSql.createIndex(newT, newI))
                .ptSql(ptSql.createIndex(newT, newI))
                .build();

    }

    @Override
    public BmScript changeIndex(DapDataModelTable newT, DapDataModelSubordinateKey oldI, DapDataModelSubordinateKey newI) {
        return BmScript.builder()
                .sqls(ddlSql.changeIndex(newT, oldI, newI))
                .ptSqls(ptSql.changeIndex(newT, oldI, newI))
                .build();
    }

}
